﻿using DTM.Model;
using System;
using System.Windows.Forms;

namespace DTM.WinApp.Players
{
    public partial class PlayerForm : Form, IPlayerView
    {
        #region Fields

        private readonly IPlayerPresenter _presenter;

        #endregion Fields

        #region Events

        public event EventHandler<EventArgs> DeletePlayer;

        public event EventHandler<EventArgs> SavePlayer;

        public event EventHandler<EventArgs> UpdatePlayer;

        #endregion Events

        #region Properties

        public string PlayerEmail
        {
            get { return txtEmail.Text; }
        }

        public int PlayerId { get; private set; }

        public string PlayerName
        {
            get { return txtName.Text; }
        }

        public string PlayerSurname
        {
            get { return txtSurname.Text; }
        }

        #endregion Properties

        #region Constructors

        public PlayerForm(IPlayerPresenter presenter)
        {
            DTM.Core.Utils.Validate.NotNull<ArgumentNullException>(presenter, "presenter");
            _presenter = presenter;

            InitializeComponent();

            _presenter.Initialize(this);
        }

        #endregion Constructors

        #region Methods

        public void Close(DialogResult result)
        {
            DialogResult = result;
        }

        public void Initialize(IPlayer player)
        {
            btnDelete.Visible = player != null;
            btnUpdate.Visible = player != null;
            btnSave.Visible = player == null;

            if (player != null)
            {
                PlayerId = player.Id;
                txtEmail.Text = player.Email;
                txtName.Text = player.Name;
                txtSurname.Text = player.Surname;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DeletePlayer != null)
            {
                DeletePlayer(this, EventArgs.Empty);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SavePlayer != null)
            {
                SavePlayer(this, EventArgs.Empty);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (UpdatePlayer != null)
            {
                UpdatePlayer(this, EventArgs.Empty);
            }
        }

        #endregion Methods
    }
}